<template>
	<jk-dialog
		v-model="showDialog"
		:title="`${dataId ? '编辑' : '新增'}-班组`"
		width="500px"
		:show-content-loading="showLoading"
		:save-audit-btn-loading="saveAuditLoading"
		:update-audit-btn-loading="updateAuditLoading"
		:save-btn-loading="saveBtnLoading"
		append-to-body
		:show-confirm-btn="false"
		:show-save-btn="true"
		:permission-prefix="permissionPrefix"
		:show-save-audit-btn="false"
		:show-update-audit-btn="false"
		@on-cancel="onVisibleChange(false)"
		@on-visible-change="onVisibleChange"
		@on-save="onConfirmBtn('save')"
		@on-save-audit="onConfirmBtn('saveAndAudit')"
		@on-update-audit="onConfirmBtn('updateAndAudit')"
	>
		<el-form ref="ruleForm" :model="ruleForm" :rules="rules" label-width="110px" :show-message="false">
			<el-row>
				<el-col :span="22">
					<el-form-item class="margin-bottom-10" label="编码" prop="code">
						<el-input v-model="ruleForm.code" placeholder="编码" />
					</el-form-item>
				</el-col>
				<el-col :span="22">
					<el-form-item class="margin-bottom-10" label="名称" prop="name">
						<el-input v-model="ruleForm.name" placeholder="名称" />
					</el-form-item>
				</el-col>
				<!--<el-col :span="22">
					<el-form-item class="margin-bottom-10" label="属性" prop="typeId">
						<el-radio-group v-model="ruleForm.typeId" size="small">
							<el-radio :label="1" border class="item-gap">前纺</el-radio>
							<el-radio :label="2" border class="item-gap">后纺</el-radio>
							<el-radio :label="0" border class="item-gap">无</el-radio>
						</el-radio-group>
					</el-form-item>
				</el-col>-->
				<el-col :span="22">
					<el-form-item class="margin-bottom-10" label="区域" prop="workshopId">
						<el-select v-model="ruleForm.workshopId" placeholder="区域" clearable class="width-100-percent">
							<el-option v-for="item in workshopList" :key="item.id" :label="item.name" :value="item.id" />
						</el-select>
					</el-form-item>
				</el-col>
				<el-col :span="22">
					<el-form-item class="margin-bottom-10" label="排序" prop="sortNum">
						<jk-number-input v-model="ruleForm.sortNum" placeholder="排序" />
					</el-form-item>
				</el-col>
			</el-row>
		</el-form>
	</jk-dialog>
</template>

<script>
    import calHeight from '/src/mixins/cal-table-height';
    import { personnelGroupCreateAudit, personnelGroupUpdateAudit, personnelGroupCreate, personnelGroupUpdate, personnelGroupGet } from '../../../api/personnel/group';
    export default {
        name: 'Group',
        props: {
            // 页面权限的前缀
            permissionPrefix: {
                type: String,
                default: ''
            },
            dialogState: {
                type: Boolean,
                default: false
            },
            dataId: {
                type: [String, Number],
                default: ''
            }
        },
        components: {
        },
        mixins: [calHeight],
        data() {
            return {
                updateAuditLoading: false,
                saveAuditLoading: false,
                saveBtnLoading: false,
                workshopList: [],
                showDialog: false,
                showLoading: true,
                confirmBtnLoading: false,
                saveDialogState: false,
                ruleForm: {
                    id: null,
                    code: null,
                    name: null,
                    status: 1,
                    sortNum: null,
                    typeId: 1,
                    workshopId: null,
                    creator: null,
                    createTime: null,
                    updater: null,
                    updateTime: null,
                    deleted: null,
                    tenantId: null
                },
                // 表单校验
                rules: {
                    typeId: [{ required: true, trigger: 'change' }],
                    code: [{ required: true, trigger: 'change' }],
                    workshopId: [{ required: true, trigger: 'change' }],
                    name: [{ required: true, trigger: 'change' }],
                    status: [{ required: true, trigger: 'change' }]
                }
            };
        },
        methods: {
            getParamData() {
                const params = JSON.parse(JSON.stringify(this.ruleForm));
                params.workshopName = this.workshopList.find(x => x.id === params.workshopId).name;
                return params;
            },
            saveAndAuditRequest() {
                this.saveAuditLoading = true;
                this.ruleForm.id = null;
                personnelGroupCreateAudit(this.getParamData()).then(res => {
                    this.saveAuditLoading = false;
                    if (res.data.status === 200) {
                        if (res.data.status === 200) {
                            this.$tipsBar(this, 'operation');
                            this.$emit('on-finish');
                        }
                    }
                });
            },
            onConfirmBtn(type) {
                this.$refs['ruleForm'].validate(valid => {
                    if (valid) {
                        if (type === 'save') {
                            this.createAndUpdateRequest();
                        } else if (type === 'saveAndAudit') {
                            this.saveAndAuditRequest();
                        } else if (type === 'updateAndAudit') {
                            this.updateAndAuditRequest();
                        }
                    } else {
                        this.$tipsBar(this, 'validator');
                    }
                });
            },
            createAndUpdateRequest() {
                this.saveBtnLoading = true;
                // 新增
                if (!this.dataId) {
                    this.ruleForm.id = null;
                    personnelGroupCreate(this.getParamData()).then(res => {
                        this.saveBtnLoading = false;
                        if (res.data.status === 200) {
                            this.$tipsBar(this, 'save');
                            this.$emit('on-finish');
                        }
                    });
                    return;
                }
                // 编辑
                personnelGroupUpdate(this.getParamData()).then(res => {
                    this.saveBtnLoading = false;
                    if (res.data.status === 200) {
                        this.$tipsBar(this, 'save');
                        this.$emit('on-finish');
                    }
                });
            },
            updateAndAuditRequest() {
                this.updateAuditLoading = true;
                personnelGroupUpdateAudit(this.getParamData()).then(res => {
                    this.updateAuditLoading = false;
                    if (res.data.status === 200) {
                        if (res.data.status === 200) {
                            this.$tipsBar(this, 'operation');
                            this.$emit('on-finish');
                        }
                    }
                });
            },
            onVisibleChange(e) {
                this.$emit('on-visible-change', e);
                if (!e) {
                    Object.assign(this.$data, this.$options.data());
                    this.$nextTick(() => {
                        this.$refs.ruleForm.resetFields();
                    });
                }
            },
            getDetailRequest() {
                return personnelGroupGet({ id: this.dataId }).then(res => {
                    if (res.data.status === 200) {
                        this.ruleForm = res.data.res;
                    }
                });
            },
            async getDependentData() {
                this.showLoading = true;
                this.workshopList = this.$store.state.user.userWorkshop.workshopList;
                if (!this.dataId) {
                    this.$set(this.ruleForm, 'workshopId', this.$store.state.user.userWorkshop.defaultWorkshopId);
                    this.$set(this.ruleForm, 'workshopCode', this.$store.state.user.userWorkshop.defaultWorkshopCode);
                    this.$set(this.ruleForm, 'workshopName', this.$store.state.user.userWorkshop.defaultWorkshopName);
                }
                if (this.dataId) await this.getDetailRequest();
                this.showLoading = false;
            }
        },
        watch: {
            dialogState(newVal) {
                this.showDialog = newVal;
                if (newVal) {
                    this.getDependentData();
                }
            }
        }
    };
</script>
